﻿using B.S.BaseData.ErrorCode;
using B.S.BaseData.ErrorCode.ErrorCodes;
using B.S.BaseData.WriteAPI.Utils;
using B.S.DataBase.EFCoreServiers.Dtos.MeetingReservations;
using B.S.DataBase.EFCoreServiers.IServices.Meetings;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BaseData.WriteAPI.Controllers
{
    /// <summary>
    /// 会议室预约管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    [ApiExplorerSettings(GroupName = "MeetingReservation")]
    public class MeetingReservationController : ControllerBase
    {
        private readonly IMeetingReservationServices _reservationServices;
        private readonly ILogger<MeetingReservationController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        public MeetingReservationController(
            IMeetingReservationServices reservationServices,
            ILogger<MeetingReservationController> logger)
        {
            _reservationServices = reservationServices;
            _logger = logger;
        }

        /// <summary>
        /// 添加会议室预约
        /// </summary>
        /// <param name="addDto">预约信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [LogOperation("会议预约管理", "新增", "添加会议室预约")]
        public async Task<ApiResult> AddReservation([FromBody] AddMeetingReservationDto addDto)
        {
            try
            {
                var result = await _reservationServices.AddReservationAsync(addDto);
                if (result.IsSuc)
                {
                    _logger.LogInformation("添加会议室预约成功: Room={RoomId}, Time={StartTime}-{EndTime}",
                        addDto.MeetingReservationId, addDto.StartTime, addDto.EndTime);
                }
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "添加会议室预约失败");
                return ApiResult.Fail("添加预约失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }


        /// <summary>
        /// 更新会议室预约
        /// </summary>
        /// <param name="updateDto">更新的预约信息</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        [LogOperation("会议预约管理", "修改", "更新会议室预约信息")]
        public async Task<ApiResult> UpdateReservation([FromBody] UpdateMeetingReservationDto updateDto)
        {
            try
            {
                var result = await _reservationServices.UpdateReservationAsync(updateDto);
                if (result.IsSuc)
                {
                    _logger.LogInformation("更新会议室预约成功: Id={Id}", updateDto.MeetingReservationId);
                }
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新会议室预约失败");
                return ApiResult.Fail("更新预约失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }

        /// <summary>
        /// 删除会议室预约
        /// </summary>
        /// <param name="id">预约ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        [LogOperation("会议预约管理", "删除", "删除会议室预约")]
        public async Task<ApiResult> DeleteReservation(int id)
        {
            try
            {
                var result = await _reservationServices.DeleteReservationAsync(id);
                if (result.IsSuc)
                {
                    _logger.LogInformation("删除会议室预约成功: Id={Id}", id);
                }
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除会议室预约失败");
                return ApiResult.Fail("删除预约失败：" + ex.Message, StatusCodeEnum.Exception);
            }
        }
    }
}
